package methodminxin.manymixin

def sendMessage(MyWriter write){
    write.write("this is groovy")
}
//创建添加过滤器的writer实例
def creeateInstance(MyWriter writer,Object[] filters =[]){
    //将过滤器注入到writer实例的mixin中
    filters.each {filter-> writer.metaClass.mixin filter}
    writer
}
//当注入多个同名同参方法时,最后加入mixin中的方法会自动隐藏掉前面的方法
// 所以默认前面的不会执行,只会执行最后一个
/**
 * 这里需要按照需求控制顺序,因为过滤器会按照顺序严格执行,比如,是先加前缀还是先转大写
 */
sendMessage(creeateInstance(new MyStringWriter(),PrefixFilter,UpperCaseFilter))
sendMessage(creeateInstance(new MyStringWriter(),UpperCaseFilter,PrefixFilter))


